﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AuchanScan.Models;
using System.Linq;

namespace AuchanScan.ViewModels
{
    public class SearchViewModel : INotifyPropertyChanged
    {

        public void Search(String search)
        {
            var list = new List<KeyValuePair<String, Achat>>();
            search = search.ToUpper();
            foreach (var histo in ViewModelLocator.MainViewModelStatic.Datas.Histories)
            {
                var achat = histo.Achats.FirstOrDefault((e) => e.Product.Name.ToUpper().Contains(search));
                if (achat != null)
                {
                    list.Add(new KeyValuePair<string, Achat>(histo.Date.ToShortDateString(), achat));
                }
            }

            Results = list;
            RaisePropertyChanged("Results");


        }


        public List<KeyValuePair<String, Achat>> Results { get; set; }


        #region PropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChanged(String name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }

        #endregion


    }
}
